package Classes.GameDatas
{
	import Classes.GameUtils.GU_DataTransform;
	import Classes.GameViews.ViewPanels.VP_Main;
	import Classes.Interfaces.I_TimeUnit;
	import Classes.Managers.M_Communication;
	import Classes.Managers.M_ConfigData;
	import Classes.Managers.M_Timer;

	public class Deal_LianJiReward implements I_TimeUnit
	{
		private static var _instance:Deal_LianJiReward;
		
		private static var _itemConfigDatasObj:Object;
		
		private static var lianJIOldTime:Number=-1;//连击原始时间
		public static var getRewardNum:Number=0;//拾取的个数
		public static var getRewardNumInLjs:int=0;//在连击达到 满数的时候 ，掉落的个数
		public static var ljLv:Number=1;//连击的次数
		public static var flag:Boolean=true;//是否开始计算拾取数目
		
		public function Deal_LianJiReward()
		{
			M_Timer.instance.joinUnit(this);
		}
		public var lj_time:Number;
		public  function opeaterReward():void
		{
			if(!flag)
			{
				getRewardNumInLjs++;
			}
			//连击可以断掉的时间
			 lj_time=itemConfigDatasObj[ljLv].zt_time;
			//在此等级需要拾取的数目
			var lj_needGetItemNum:Number=itemConfigDatasObj[ljLv].zt_need;
			//每拾取一个跳动的帧数
			var jampFramePer:int=100/lj_needGetItemNum;
//			var jampFramePer:Number=100/2;
			//连击功能  
			if(lianJIOldTime==-1)
			{
				lianJIOldTime=M_Timer.instance.nowTime;
				innerLjFun(jampFramePer);
			}else{
				if((M_Timer.instance.nowTime-lianJIOldTime)<=lj_time)
				{
					lianJIOldTime=M_Timer.instance.nowTime;
					innerLjFun(jampFramePer);
				}else{
					lianJIOldTime=-1;
					getRewardNum=0;
					getRewardNumInLjs=0;
					ljLv=1;
					VP_Main.instance.hanldLianJIFun(false,1,ljLv);
				}
			}
		}
		
		private  function innerLjFun(jampFramePer:int):void
		{
			if(!flag)return;
			getRewardNum+=1;
			var tmpJamp:int=getRewardNum*jampFramePer;
			if(tmpJamp>=100)
			{
				flag=false;
				tmpJamp=VP_Main.instance.ljMc.totalFrames;
				VP_Main.instance.hanldLianJIFun(true,tmpJamp,ljLv,callFun);
			}else{
				VP_Main.instance.hanldLianJIFun(true,tmpJamp,ljLv);
			}
		}
		
		private  function callFun():void
		{
			getRewardNum=0;
			getRewardNum=getRewardNumInLjs;
			getRewardNumInLjs=0;
			ljLv+=1;
			flag=true;
			VP_Main.instance.hanldLianJIFun(true,1,ljLv);
			M_Communication.instance.send(12305,{levelID:ljLv-1});
			if(getRewardNum>0)this.opeaterReward();
		}
		
		

		public static function get itemConfigDatasObj():Object
		{
			if(!_itemConfigDatasObj)
			{
				_itemConfigDatasObj=new Object;
				var itemConfigDatasStr:String=M_ConfigData.instance.getCSVByName("连锁静态表");
				var itemConfigDatasAry:Array=GU_DataTransform.csvTransformObjsAry(itemConfigDatasStr,4);
				for each(var itemConfigDataObj:Object in itemConfigDatasAry)
				{
					_itemConfigDatasObj[itemConfigDataObj.zt_chain_level]=itemConfigDataObj;
				}
			}
			return _itemConfigDatasObj;
		}
		
		public function timeUpdate():void
		{
			if(lianJIOldTime!=-1)
			{
				if((M_Timer.instance.nowTime-lianJIOldTime)>lj_time)
				{
					lianJIOldTime=-1;
					getRewardNum=0;
					getRewardNumInLjs=0;
					ljLv=1;
					VP_Main.instance.hanldLianJIFun(false,1,ljLv);
				}
			}
		}
		
		public function get isUpdateTime():Boolean
		{
			return true;
		}
		public function set isUpdateTime(value:Boolean):void
		{
		}
		
		public static function get instance():Deal_LianJiReward
		{
			if(!_instance)_instance=new Deal_LianJiReward();
			return _instance;
		}

	}
}